package com.lq.controller;

import com.lq.pojo.OverallSetting;
import com.lq.pojo.PersonalSetting;
import com.lq.pojo.User;
import com.lq.service.SettingService;
import com.lq.util.SystemLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class SettingController {

    @Autowired
    private SettingService settingService;

    @RequestMapping("/getSetting")
    public String getSetting(HttpSession session, Model model) {

        User user = (User) session.getAttribute("user");

        PersonalSetting personalSetting = settingService.getPersonalSettingByUserId(user.getUserId());
        OverallSetting overallSetting = settingService.getOverallSettingByUserId(user.getUserId());

        model.addAttribute("personalSetting", personalSetting);
        model.addAttribute("overallSetting", overallSetting);

        return "setting";
    }

    @RequestMapping("/saveOverallSetting")
    public String saveOverallSetting(String siteName, String siteDescribe, String keyWord, Integer theme, HttpSession session) throws ServletException, IOException {

        User user = (User) session.getAttribute("user");

        OverallSetting overallSetting = new OverallSetting(
                0,
                siteName,
                siteDescribe,
                keyWord,
                theme,
                user.getUserId()
        );


        //保存全局设置
        settingService.saveOverallSetting(overallSetting);

        SystemLogUtil.editSettingLog();

        return "forward:/getSetting";
    }


    @RequestMapping("/savePersonalSetting")
    public String savePersonalSetting(PersonalSetting personalSetting, HttpSession session) throws ServletException, IOException {

        User user = (User) session.getAttribute("user");

        PersonalSetting setting = personalSetting;

        setting.setUserId(user.getUserId());


        settingService.savePersonalSetting(setting);

        SystemLogUtil.editSettingLog();

        return "forward:/getSetting";

    }
}
